﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using Ccit.Utils;

namespace Ccit.OA.NewsCenter
{
    public partial class news_submit_addpic : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdId.Value = (null != Request.QueryString["Id"]) ? Request.QueryString["Id"].ToString() : "";
                hdType.Value = (null != Request.QueryString["Type"]) ? Request.QueryString["Type"].ToString() : "0";
                hdRefreshOpener.Value = (null != Request.QueryString["RefreshOpener"]) ? Request.QueryString["RefreshOpener"].ToString() : "0";  //0：不刷新Opener；1：刷新Opener
                hdFuncName.Value = (null != Request.QueryString["FuncName"]) ? Request.QueryString["FuncName"].ToString() : "";  //要执行刷新的方法名
                string sRelatingId = hdId.Value;
                string sRelatingType = hdType.Value;
                SubmitBLL oSubmit = new SubmitBLL();
                MsImage msImage = oSubmit.GetImageByNewsId(sRelatingId, sRelatingType);
                hdPicId.Value = msImage.PicId;
                InitPage();
            }
        }

        private void InitPage()
        {
            string sPicId = hdPicId.Value;
            if (sPicId == "0"||sPicId=="")
            {
                btnDelete.Disabled = true;
                btnUpLoad.Enabled = true;
                imgPreview.Attributes.Remove("Src");
            }
            else
            {
                btnUpLoad.Enabled = false;
                btnDelete.Disabled = false;
                string sPageParam="tablename=NEWS_A5&keyfield=NEWS_A5_10&keyfieldvalue=" + sPicId + "&imagefield=NEWS_A5_20";
                string sEncodeParam=Base64Lib.Encode(sPageParam);
                imgPreview.Src = "../UserControls/attach_image_show.aspx?" + sEncodeParam;
            }
        }

        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            string sRelatingId = hdId.Value;
            //string sRelatingName = SysInterface.GetNameStrByIdStr("NEWS_A3", "NEWS_A3_10", "NEWS_A3_30", sNewsId, "");
            SubmitBLL oSubmit=new SubmitBLL();
            if (upImage.PostedFile.FileName != "")
            {
                int iImageSize = upImage.PostedFile.ContentLength;
                Byte[] oByte = new Byte[iImageSize];
                System.IO.Stream oStream = upImage.PostedFile.InputStream;
                oStream.Read(oByte, 0, iImageSize);
                MsImage msImage = new MsImage();
                msImage.PicContent = oByte;
                msImage.PicSize = iImageSize.ToString();
                msImage.PicType = upImage.PostedFile.ContentType;
                msImage.PicDesc = "";
                msImage.RelatingId = hdId.Value;
                msImage.RelatingType = hdType.Value;
                string sPicId = oSubmit.UploadImageByNewsId(msImage, sRelatingId);
                if (sPicId != "")
                {
                    hdPicId.Value = sPicId;
                    Response.Write("<script language='javascript'>alert('图片上传成功！');</script>");
                    InitPage();
                    if(hdRefreshOpener.Value=="1")
                        Response.Write("<script language='javascript'>eval(\"window.opener."+hdFuncName.Value+"()\");</script>");
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('图片上传失败，请重新尝试！');</script>");
                }
            }
            else
            {
                Response.Write("<script language='javascript'>alert('请选择要上传的图片！');</script>");
            }
        }

        [global::System.Web.Services.WebMethod]
        public static bool DeleteImage(string sPicId, string sId, string sType)
        {
            SubmitBLL oSubmit = new SubmitBLL();
            bool bFlag = oSubmit.DeleteImage(sPicId, sId, sType);
            return bFlag;
        }

        protected void likRefresh_Click(object sender, EventArgs e)
        {
            InitPage();
        }
    }
}
